package cn.com.lsy.product.lepao.controller;

import cn.com.lsy.product.lepao.controller.base.BaseController;
import cn.com.lsy.product.lepao.model.entities.LpTrainTypeVideoTbl;
import cn.com.lsy.product.lepao.model.entities.LpTrainVideoChildTbl;
import cn.com.lsy.product.lepao.model.entities.LpTrainVideoLabelTbl;
import cn.com.lsy.product.lepao.model.mt.entities.TrainTypeVideoListDto;
import cn.com.lsy.product.lepao.request.*;
import cn.com.lsy.product.lepao.response.ActionLibraryInfoRet;
import cn.com.lsy.product.lepao.response.ActionLibraryListRet;
import cn.com.lsy.product.lepao.response.TrainLabelRet;
import cn.com.lsy.product.lepao.response.base.ApiResponseEnum;
import cn.com.lsy.product.lepao.response.base.BaseRet;
import cn.com.lsy.product.lepao.response.base.PageInfo;
import cn.com.lsy.product.lepao.service.ITrainCenterService;
import cn.com.lsy.product.lepao.util.DozerMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(value = "训练中心接口", tags = "训练中心接口")
@RestController
@RequestMapping(value = "/lepao/console/doAuth")
public class TrainCenterController extends BaseController {

    private Logger log = Logger.getLogger(TrainCenterController.class);
    Mapper mapper = new DozerBeanMapper();


    @Autowired
    ITrainCenterService iTrainCenterService;

    @RequestMapping(value = "/actionLibraryList", method = RequestMethod.POST)
    @ApiOperation(notes = "动作库列表", httpMethod = "POST", value = "动作库列表")
    public BaseRet<PageInfo<List<ActionLibraryListRet>>> actionLibraryList(
            @ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryListParam param,
            BindingResult result) {

        BaseRet<PageInfo<List<ActionLibraryListRet>>> ret = new BaseRet<PageInfo<List<ActionLibraryListRet>>>();

        PageInfo<List<ActionLibraryListRet>> pageInfo = new PageInfo<List<ActionLibraryListRet>>();

        try {

            checkParam(result);

            int count = iTrainCenterService.selectActionLibraryByCount(param.getSearchCondition(), param.getType());
            pageInfo.setCount(count);
            int start = param.getStart();

            List<TrainTypeVideoListDto> list = iTrainCenterService.selectActionLibraryByPage(start, param.getPageSize(), param.getSearchCondition(), param.getType());

            List<ActionLibraryListRet> listRet = new ArrayList<ActionLibraryListRet>();

            if (list != null && list.size() > 0) {

                for (TrainTypeVideoListDto value : list) {

                    ActionLibraryListRet actionLibraryListRet = new ActionLibraryListRet();
                    mapper.map(value, actionLibraryListRet);
                    listRet.add(actionLibraryListRet);
                }
            }

            pageInfo.setRecords(listRet);

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(pageInfo);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/actionLibraryInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "动作库详情", httpMethod = "POST", value = "动作库详情")
    public BaseRet<ActionLibraryInfoRet> actionLibraryInfo(
            @ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param,
            BindingResult result) {

        BaseRet<ActionLibraryInfoRet> ret = new BaseRet<ActionLibraryInfoRet>();

        ActionLibraryInfoRet actionLibraryInfoRet = new ActionLibraryInfoRet();

        try {

            checkParam(result);

            LpTrainTypeVideoTbl info = iTrainCenterService.selectActionLibraryInfo(param.getId());

            mapper.map(info, actionLibraryInfoRet);

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(actionLibraryInfoRet);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateActionLibrary", method = RequestMethod.POST)
    @ApiOperation(notes = "修改动作库信息", httpMethod = "POST", value = "修改动作库信息")
    public BaseRet<String> updateActionLibrary(
            @ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateActionLibraryParam param,
            BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpTrainTypeVideoTbl lpTrainTypeVideoTbl = new LpTrainTypeVideoTbl();
            mapper.map(param, lpTrainTypeVideoTbl);
            lpTrainTypeVideoTbl.setIsScan(param.getIsScan());

            if (iTrainCenterService.updateActionLibrary(lpTrainTypeVideoTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addActionLibrary", method = RequestMethod.POST)
    @ApiOperation(notes = "添加动作库信息", httpMethod = "POST", value = "添加动作库信息")
    public BaseRet<String> addActionLibrary(
            @ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddActionLibraryParam param,
            BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpTrainTypeVideoTbl lpTrainTypeVideoTbl = new LpTrainTypeVideoTbl();
            mapper.map(param, lpTrainTypeVideoTbl);

            if (iTrainCenterService.insertActionLibrary(lpTrainTypeVideoTbl)) {

                ret.setMsg("添加成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("添加失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/trainLabelList", method = RequestMethod.POST)
    @ApiOperation(notes = "动作库标签列表", httpMethod = "POST", value = "动作标签列表")
    public BaseRet<List<TrainLabelRet>> TrainLabelList() {

        BaseRet<List<TrainLabelRet>> ret = new BaseRet<List<TrainLabelRet>>();

        try {


            List<LpTrainVideoLabelTbl> list = iTrainCenterService.selectTrainVideoLabel();
            List<TrainLabelRet> listRet = DozerMapperUtil.mapList(list, TrainLabelRet.class);

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(listRet);

        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/trainLabelInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "动作库标签详情", httpMethod = "POST", value = "动作库标签详情")
    public BaseRet<TrainLabelRet> trainLabelInfo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param,
                                                 BindingResult result) {

        BaseRet<TrainLabelRet> ret = new BaseRet<TrainLabelRet>();

        try {

            LpTrainVideoLabelTbl info = iTrainCenterService.selectTrainVideoLabelInfo(param.getId());

            TrainLabelRet infoRet = DozerMapperUtil.map(info, TrainLabelRet.class);

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(infoRet);

        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateTrainLabel", method = RequestMethod.POST)
    @ApiOperation(notes = "修改动作库标签", httpMethod = "POST", value = "修改动作库标签")
    public BaseRet<String> updateTrainLabel(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateTrainLabelParam param,
                                            BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            LpTrainVideoLabelTbl lpTrainVideoLabelTbl = new LpTrainVideoLabelTbl();
            lpTrainVideoLabelTbl.setName(param.getName());
            lpTrainVideoLabelTbl.setId(param.getId());

            if (iTrainCenterService.updateTrainVideoLabel(lpTrainVideoLabelTbl)) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("修改成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("修改失败");

            }

        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addTrainLabel", method = RequestMethod.POST)
    @ApiOperation(notes = "添加动作库标签", httpMethod = "POST", value = "添加动作库标签")
    public BaseRet<String> addTrainLabel(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddTrainLabelParam param,
                                         BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            LpTrainVideoLabelTbl lpTrainVideoLabelTbl = new LpTrainVideoLabelTbl();
            lpTrainVideoLabelTbl.setName(param.getName());

            if (iTrainCenterService.insertTrainVideoLabel(lpTrainVideoLabelTbl)) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("添加成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("添加失败，名称可能已存在");

            }

        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/delTrainLabel", method = RequestMethod.POST)
    @ApiOperation(notes = "删除动作库标签", httpMethod = "POST", value = "删除动作库标签")
    public BaseRet<String> delTrainLabel(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param,
                                         BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {


            if (iTrainCenterService.delTrainVideoLabel(param.getId())) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("删除成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("删除失败");
            }


        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/actionLibraryChildInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "动作库子类详情", httpMethod = "POST", value = "动作库子类详情")
    public BaseRet<ActionLibraryInfoRet> actionLibraryChildInfo(
            @ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param,
            BindingResult result) {

        BaseRet<ActionLibraryInfoRet> ret = new BaseRet<ActionLibraryInfoRet>();

        ActionLibraryInfoRet actionLibraryInfoRet = new ActionLibraryInfoRet();

        try {

            checkParam(result);

            LpTrainVideoChildTbl info = iTrainCenterService.selectActionLibraryChildInfo(param.getId());

            if (info != null) {
                mapper.map(info, actionLibraryInfoRet);
            }

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(actionLibraryInfoRet);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addActionLibraryChild", method = RequestMethod.POST)
    @ApiOperation(notes = "添加动作库子类信息", httpMethod = "POST", value = "添加动作库子类信息")
    public BaseRet<String> addActionLibraryChild(
            @ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddActionLibraryChildParam param,
            BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpTrainVideoChildTbl lpTrainVideoChildTbl = new LpTrainVideoChildTbl();
            mapper.map(param, lpTrainVideoChildTbl);

            if (iTrainCenterService.insertActionLibraryChild(lpTrainVideoChildTbl)) {

                ret.setMsg("添加成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("添加失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateActionLibraryChild", method = RequestMethod.POST)
    @ApiOperation(notes = "修改动作库子类信息", httpMethod = "POST", value = "修改动作库子类信息")
    public BaseRet<String> updateActionLibraryChild(
            @ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateActionLibraryParam param,
            BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpTrainVideoChildTbl lpTrainVideoChildTbl = new LpTrainVideoChildTbl();
            mapper.map(param, lpTrainVideoChildTbl);

            if (iTrainCenterService.updateActionLibraryChild(lpTrainVideoChildTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/delActionLibraryChild", method = RequestMethod.POST)
    @ApiOperation(notes = "删除动作库子类信息", httpMethod = "POST", value = "删除动作库子类信息")
    public BaseRet<String> delActionLibraryChild(
            @ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param,
            BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            iTrainCenterService.delActionLibraryChild(param.getId());

            ret.setMsg("删除成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);


        } catch (Exception e)

        {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }

}
